<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/6 0006
 * Time: 10:05
 */

namespace Wx\Controller;


use Think\Controller;

class DetectionController extends Controller
{
    public function index(){
        $this->foretaste();//调用检测活动

        $this->wholesale();//调用检测批发

        $this->exchange();//调用检测积分兑换

    }//end fun

    //试吃活动
    public function foretaste(){

        $goods = M("foretaste_goods")->where("state = 1")->select();//所有商品

        foreach($goods as $gk => $gv){
            //活动商品--库存为0结束
            if ($gv['num'] > 0){
                // 活动商品——活动时间内
                if (time() <= $gv['endtime']) {

                    //获取该商品活动的商品订单--完成集赞的
                    $order = M("foretaste_order")->where("g_id = {$gv['id']}  AND population >= {$gv['praise_num']} AND state = 0")->select();

                    //订单处理
                    foreach ($order as $ok => $ov){
                        //申请中的订单
                        $usetime = time()-$ov['addtime'];//参与时间
                        M("foretaste_order")->where("id = {$ov['id']}")->save(['endtime'=>time(),'state'=>1,'usetime'=>$usetime]);//完成点赞，自动成功
                        M('foretaste_goods')->where("id = {$ov['g_id']}")->setDec('num',1);//减库存

                        M("voice")->add(['u_id'=>$ov['u_id'],'content'=>'您成功参与了试吃活动，请等待发货通知!','sendtime'=>time()]);
                        D("SendMsg")->sendsuccess($ov['u_id'],'试吃','试吃申请',date("Y-m-d H:i:s",$ov['addtime']),"点击查看详情>>>>>","http://www.xiangtws.com/index.php/Wx/ForetasteOrder/order/oid/".$ov['id']);

                    }//end foreach

                }else{
                    //获取该商品活动的商品订单--未完成集赞的
                    $order = M("foretaste_order")->where("g_id = {$gv['id']} AND state = 0")->select();
                    //订单处理
                    foreach ($order as $ok => $ov){
                        //申请中的订单
                        M("foretaste_order")->where("id = {$ov['id']}")->save(['state'=>6]);//完成点赞，自动失败
                        $save_credit = M('member')->where("id = {$ov['u_id']}")->setInc('credit1',floatval($ov['price']));//退积分
                        if ($save_credit){
                            //积分记录
                            $credit_log = array(
                                'u_id' => $ov['u_id'],
                                'credit_type' => 'credit1',
                                'change_number' => $ov['price'],
                                'change_type' => '+',
                                'reason' => '试吃活动失败返还',
                                'changetime' => time()
                            );
                            M("member_credit_log")->add($credit_log);//积分记录
                            M("voice")->add(['u_id'=>$ov['u_id'],'content'=>'您参与的试吃活动失败,积分已退回到您的账户!','sendtime'=>time()]);
                            D("SendMsg")->senderror($ov['u_id'],'试吃','试吃申请',"申请失败","您参与的试吃活动失败!","点击查看详情>>>>>","http://www.xiangtws.com/index.php/Wx/ForetasteOrder/order/oid/".$ov['id']);
                            M("voice")->add(['u_id'=>$ov['u_id'],'content'=>'试吃活动失败，退回'.$ov['price'].'积分','sendtime'=>time()]);
                        }//end if
                    }//end foreach $order
                    M("foretaste_goods")->where("id = {$gv['id']}")->save(['state' => 4]);//结束活动
                }//end if $gv.endtime
            }else{
                //获取该商品活动的商品订单--未完成集赞的
                $order = M("foretaste_order")->where("g_id = {$gv['id']} AND state = 0")->select();
                //订单处理
                foreach ($order as $ok => $ov){
                    //申请中的订单
                    M("foretaste_order")->where("id = {$ov['id']}")->save(['state'=>6]);//完成点赞，自动失败
                    $save_credit = M('member')->where("id = {$ov['u_id']}")->setInc('credit1',floatval($ov['price']));//退积分
                    if ($save_credit){
                        //积分记录
                        $credit_log = array(
                            'u_id' => $ov['u_id'],
                            'credit_type' => 'credit1',
                            'change_number' => $ov['price'],
                            'change_type' => '+',
                            'reason' => '试吃活动失败返还',
                            'changetime' => time()
                        );
                        M("member_credit_log")->add($credit_log);//积分记录
                        M("voice")->add(['u_id'=>$ov['u_id'],'content'=>'您参与的试吃活动失败,积分已退回到您的账户!','sendtime'=>time()]);
                        D("SendMsg")->senderror($ov['u_id'],'试吃','试吃申请',"申请失败","您参与的试吃活动失败!","点击查看详情>>>>>","http://www.xiangtws.com/index.php/Wx/ForetasteOrder/order/oid/".$ov['id']);
                        M("voice")->add(['u_id'=>$ov['u_id'],'content'=>'试吃活动失败，退回'.$ov['price'].'积分','sendtime'=>time()]);
                    }//end if
                }//end foreach $order
                M("foretaste_goods")->where("id = {$gv['id']}")->save(['state' => 4]);//结束活动
            }//end if $gv.num
        }//end foreach $goods

        //扶贫大使活动时间-start
        $helppoor = M("helppoor")->select();
        if (isset($helppoor)){
            foreach ($helppoor as $hk => $hv){
                if (time() >= $hv['starttime'] && $hv['state'] == 0){
                    M("helppoor")->where("id = {$hv['id']}")->save(['state'=>1]);//开始排名
                }//end if
                if (time() > $hv['endtime'] && $hv['state'] == 1){
                    M("helppoor")->where("id = {$hv['id']}")->save(['state'=>2]);//结束排名
                    $ranking_info = M("foretaste_ranking")->where("h_id = {$hv['id']}")->order('eva_num desc')->select();//查看排名
                    $member_id = array();
                    foreach ($ranking_info as $k => $v){
                        if (!array_key_exists($v['c_id'],$member_id)){
                            $member_id[$v['c_id']]=$v['u_id'];//获取小区第一
                            M("voice")->add(['u_id'=>$v['u_id'],'content'=>'恭喜您成为扶贫大使，获得恩施三天游的机会!','sendtime'=>time()]);//通知
                            D("SendMsg")->sendsuccess($v['u_id'],'扶贫排行','扶贫大使排行，恩施3日游',date("Y-m-d H:i:s",time()),"点击查看详情>>>>>","http://www.xiangtws.com/index.php/Wx/Foretaste/helppoor");
                        }//end if
                    }//end foreach
                    $member_id=implode(',',array_slice($member_id,0,$hv['success_num']));//转换为字符串
                    //处理多余的逗号
                    $member_id=preg_replace('/(,)+/i',',',$member_id);
                    $member_id=rtrim($member_id,',');//去尾
                    $member_id=ltrim($member_id,',');//去头
                    M("helppoor")->where("id = {$hv['id']}")->save(['member_ranking'=>$member_id]);//成功用户排名
                }//end if
            }//end foreach
        }//end if
        //扶贫大使活动时间-end

        //未关注的存储到xcache中，程序轮询检测（关注后推送最近浏览的商品信息）-start
        if(session('focus')==0 && session('u_id')>0){
            $focusmsg=new \Wx\Controller\AutofocusmsgController();  //用绝对路径找
            $focusmsg->setxcache(session('u_id'),"参与试吃活动",'http://www.xiangtws.com'.$_SERVER['REQUEST_URI']);
        }//end if
        //未关注的存储到xcache中，程序轮询检测（关注后推送最近浏览的商品信息）-end
    }//end fun

    //批发
    public function wholesale(){

        //获取支付截止时间
        $pay_log_info = M("wholesale_pay_log")->where("state = 0")->field('id,addtime')->select();//获取所有支付记录

        foreach($pay_log_info as $pk => $pv){
            $deadline = strtotime(date('YmdHis',$pv['addtime'])." +1 hours")-time();
            $deadline = $deadline>0?$deadline:0;

            //支付状态更改
            if($deadline == 0){
                M("wholesale_pay_log")->where("id = {$pv['id']} AND state = 0")->save(['state'=>2]);//已失效
                $w_info = M("wholesale")->where("pay_log_id = {$pv['id']}")->field('id,g_id,num')->select();//订单id
                foreach ($w_info as $wk => $wv){
                    M("wholesale")->where("id = {$wv['id']}")->save(['state'=>1]);//订单已失效
                    M("shop_spellgroup")->where("id = {$wv['g_id']}")->setInc('num',$wv['num']);//退还库存
                }//end foreach
            }//end if
        }//end foreach

    }//end fun

    //积分兑换
    public function exchange(){

        //获取支付截止时间
        $order_info = M("exchange_order")->where("state = 0")->select();//获取所有订单记录

        foreach($order_info as $pk => $pv){
            $deadline = strtotime(date('YmdHis',$pv['addtime'])." +1 hours")-time();
            $deadline = $deadline>0?$deadline:0;

            //支付状态更改
            if($deadline == 0){
                M("exchange_order")->where("id = {$pv['id']} AND state = 0")->save(['state'=>4]);//已失效
                M("exchange_goods")->where("id = {$pv['g_id']}")->setInc('num',1);//退还库存
            }//end if
        }//end foreach

    }//end fun
}//end fun